const utils = {
	/**
	 * @description 复制数据
	 * @param {String} data
	 */
	copy(data) {
		if (!data) return false
		uni.setClipboardData({
			data: data.toString()
		})
	},
	/**
	 * @description 跳转网址
	 */
	jumpUrl(url) {
		if (!url) return
		// #ifdef APP-PLUS
		plus.runtime.openURL(url)
		// #endif
		// #ifdef H5
		location.href = url
		// #endif
	},
	/**
	 * 加法精度计算
	 * @author 深圳前海万联科技有限公司 <www.wanlshop.com>
	 * 
	 * @param {Object} number 数字
	 */
	bcadd(a, b) {
		var c, d, e;
		try {
			c = a.toString().split(".")[1].length
		} catch (f) {
			c = 0
		}
		try {
			d = b.toString().split(".")[1].length
		} catch (f) {
			d = 0
		}
		return e = Math.pow(10, Math.max(c, d)), (this.bcmul(a, e) + this.bcmul(b, e)) / e
	},

	/**
	 * 减法精度计算
	 * @author 深圳前海万联科技有限公司 <www.wanlshop.com>
	 * 
	 * @param {Object} number 数字
	 */
	bcsub(a, b) {
		var c, d, e;
		try {
			c = a.toString().split(".")[1].length
		} catch (f) {
			c = 0
		}
		try {
			d = b.toString().split(".")[1].length
		} catch (f) {
			d = 0
		}
		return e = Math.pow(10, Math.max(c, d)), (this.bcmul(a, e) - this.bcmul(b, e)) / e
	},

	/**
	 * 乘法精度计算
	 * @author 深圳前海万联科技有限公司 <www.wanlshop.com>
	 * 
	 * @param {Object} number 数字
	 */
	bcmul(a, b) {
		var c = 0,
			d = a.toString(),
			e = b.toString();
		try {
			c += d.split(".")[1].length
		} catch (f) {}
		try {
			c += e.split(".")[1].length
		} catch (f) {}
		return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c)
	},

	/**
	 * 除法精度计算
	 * @author 深圳前海万联科技有限公司 <www.wanlshop.com>
	 * 
	 * @param {Object} number 数字
	 */
	bcdiv(a, b) {
		var c, d, e = 0,
			f = 0;
		try {
			e = a.toString().split(".")[1].length
		} catch (g) {}
		try {
			f = b.toString().split(".")[1].length
		} catch (g) {}
		return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), this.bcmul(c /
			d, Math.pow(10, f - e))
	},
	download(res) {
		const filePath = res.url;
		const tempLink = document.createElement("a");
		tempLink.style.display = "none";
		tempLink.href = filePath;
		tempLink.setAttribute("download", res.fileName);
		tempLink.setAttribute("target", "_blank");
		document.body.appendChild(tempLink);
		tempLink.click();
		document.body.removeChild(tempLink);
	},
	formatFileSize(fileSize=0) {
	    if (fileSize < 1024) {
	        return fileSize + 'B';
	    } else if (fileSize < (1024*1024)) {
	        var temp = fileSize / 1024;
	        temp = temp.toFixed(2);
	        return temp + 'KB';
	    } else if (fileSize < (1024*1024*1024)) {
	        var temp = fileSize / (1024*1024);
	        temp = temp.toFixed(2);
	        return temp + 'MB';
	    } else {
	        var temp = fileSize / (1024*1024*1024);
	        temp = temp.toFixed(2);
	        return temp + 'GB';
	    }
	}
}

export default utils;